Converting Types

Objectives


Discussion

Converting Data

Dim s as String = "3" 
Dim i as Integer
i = cint(s) 

Input Boxes

Dim sName as String
sName = InputBox("Enter your name")

input box

Back to top


Demonstration

1.  Add a new form to your Unit 2 project and make it the startup form.

2. Add a button (btnGetAndAdd) and add the following to the click event:

Dim X As String
Dim Y As String
X = InputBox("Enter a number")
Y = InputBox("Enter another number")
MessageBox.Show(X + Y)

3.  Run the program, enter numbers when asked, and observe the result in the messagebox.  Notice that the numbers are being added as strings.

4.  Modify the code by changing the last line above to:

MessageBox.Show(cint(X) + cint(Y))

5.  In this line, we are using the conversion function cint to convert X and Y to integers so that they are added as numbers.  Run the program and enter numbers.

6.  We are going to modify the code to be very compact.  The new compact code will do the same as the above code but with only one line. Comment out all of the code above and type in the following in the click event. Your code should all be on one line or use an underscore to break it into two lines.

MessageBox.Show(CInt(InputBox("Enter a number")) + CInt(InputBox("Enter another number")))

7. Run your program and click the button.  This line of code replaced the five previous.  The inputboxes return strings, these strings are converted to integers, the integers are added, and the result is displayed in the messagebox.  Study this code and make sure that you understand it.

8.  Notice when you type in "InputBox("  VS provides a tip about what information needs to be passed into the function.  The items in brackets ([ ]) are optional.

Back to top
Exercises

1.  Write a program that will add two numbers as either integers, singles, or strings.   Your program should meet the following specifications:

  1. It should have three buttons (btnAddAsIntegers, btnAddAsSingles, btnAddAsStrings).
  2. When the user clicks a button, two InputBoxes should appear in sequence requesting the user to enter numbers. 
  3. The first InputBox should appear on the left side of the screen and the second InputBox should appear on the right side of the screen.
  4. Each InputBox should have an appropriate title.
  5. Each InputBox should be displayed with default values of "0" already in them.
  6. After the user clicks the OK button on the second InputBox, the two values (from the InputBoxes) should be added as the appropriate type (Integer, Single, String) depending on the button clicked. The result should be displayed in a messagebox.
  7. The caption in the messagebox should indicate how the numbers were added (as Integers, Singles, or Strings).
  8. The message should display the answer in the form:  "Num1 + Num2 = Answer".  For example, if the user enters 23.45 for number 1 and 34.6667 for number 2 and adds them as Integers the messagebox should look like:

         exercise

2. Write statements to complete the following:

  1. Declare a variable named dX as a Double
  2. Declare a variable named iX as an Integer
  3. Use CType to convert iX to a Double and assign the result to dX.
Back to top

Links & Help
Back to top